package com.bicon.botu.core.base.repository.mybatis;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import com.bicon.botu.core.base.common.Constants;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 实体基类（自增主键）
 *
 * @author tandk
 * @date 2017/6/13 0013 下午 4:45
 */
@Data
public class AutoIdBaseEntity<T extends AutoIdBaseEntity> extends Model<T> implements Cloneable {
    /**
     * 主键
     */
    @TableId(type = IdType.AUTO)
    private int id;
    /**
     * 删除标志：0正常，1删除
     */
    @TableField("logic_delete")
    @JSONField(name = "logic_delete")
    private String logicDelete;

    /**
     * 创建时间
     */
    @TableField("create_date")
    @JSONField(name = "create_date")
    private Date createDate;

    /**
     * 更新时间
     */
    @TableField("update_date")
    @JSONField(name = "update_date")
    private Date updateDate;

    /**
     * 预添加
     */
    public void preInsert() {
        this.logicDelete = Constants.DEL_LOGIC_NORMAL;
        this.createDate = new Date();
        this.updateDate = this.createDate;
    }

    /**
     * 预修改
     */
    public void preUpdate() {
        this.updateDate = new Date();
    }

    /**
     * 预逻辑删除
     */
    public void preDelete() {
        this.logicDelete = Constants.DEL_LOGIC_DELETE;
        this.updateDate = new Date();
    }

    /**
     * 主键
     * @return
     */
    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    /**
     * 克隆
     * @return
     */
    @Override
    public Object clone() {
        T t = null;
        try {
            t = (T) super.clone();
        } catch (CloneNotSupportedException var3) {
            var3.printStackTrace();
        }
        return t;
    }
}
